// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "user_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "用户ID",
    "defaultValue": {
      "$env": "uid"
    },
    "label": "用户ID"
  },
  "order_type": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "全新商品",
            "value": 0
          },
          {
            "text": "转售商品",
            "value": 1
          },
          {
            "text": "混合订单",
            "value": 2
          }
        ]
      }
    ],
    "title": "订单类型",
    "defaultValue": 0,
    "label": "订单类型"
  },
  "order_status": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "待付款",
            "value": 0
          },
          {
            "text": "待发货",
            "value": 1
          },
          {
            "text": "待收货",
            "value": 2
          },
          {
            "text": "待评价",
            "value": 3
          },
          {
            "text": "已完成",
            "value": 4
          },
          {
            "text": "已取消",
            "value": 5
          },
          {
            "text": "退款中",
            "value": 6
          },
          {
            "text": "已退款",
            "value": 7
          }
        ]
      }
    ],
    "title": "订单状态",
    "defaultValue": 0,
    "label": "订单状态"
  },
  "pay_status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "未支付",
            "value": 0
          },
          {
            "text": "已支付",
            "value": 1
          },
          {
            "text": "部分退款",
            "value": 2
          },
          {
            "text": "全额退款",
            "value": 3
          }
        ]
      }
    ],
    "title": "支付状态",
    "defaultValue": 0,
    "label": "支付状态"
  },
  "address_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "收货地址ID",
    "label": "收货地址ID"
  },
  "order_items": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "订单商品",
    "label": "订单商品"
  },
  "order_tradecodes": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "商品tradecode",
    "label": "商品tradecode"
  },
  "shop_groups": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "店铺分组",
    "label": "店铺分组"
  },
  "total_amount": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "number"
      }
    ],
    "title": "商品总金额",
    "label": "商品总金额"
  },
  "shipping_fee": {
    "rules": [
      {
        "format": "number"
      }
    ],
    "title": "运费",
    "defaultValue": 0,
    "label": "运费"
  },
  "discount_amount": {
    "rules": [
      {
        "format": "number"
      }
    ],
    "title": "优惠金额",
    "defaultValue": 0,
    "label": "优惠金额"
  },
  "final_amount": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "number"
      }
    ],
    "title": "实付金额",
    "label": "实付金额"
  },
  "payment_method": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "支付方式",
    "label": "支付方式"
  },
  "pay_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "支付时间",
    "label": "支付时间"
  },
  "ship_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "发货时间",
    "label": "发货时间"
  },
  "receive_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "收货时间",
    "label": "收货时间"
  },
  "finish_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "完成时间",
    "label": "完成时间"
  },
  "cancel_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "取消时间",
    "label": "取消时间"
  },
  "cancel_reason": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "取消原因",
    "label": "取消原因"
  },
  "remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "订单备注",
    "label": "订单备注"
  },
  "create_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "创建时间",
    "defaultValue": {
      "$env": "now"
    },
    "label": "创建时间"
  },
  "expire_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "过期时间",
    "label": "过期时间"
  },
  "evaluate_status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "未评价",
            "value": 0
          },
          {
            "text": "已评价",
            "value": 1
          }
        ]
      }
    ],
    "title": "评价状态",
    "defaultValue": 0,
    "label": "评价状态"
  }
}

const enumConverter = {
  "order_type_valuetotext": {
    "0": "全新商品",
    "1": "转售商品",
    "2": "混合订单"
  },
  "order_status_valuetotext": {
    "0": "待付款",
    "1": "待发货",
    "2": "待收货",
    "3": "待评价",
    "4": "已完成",
    "5": "已取消",
    "6": "退款中",
    "7": "已退款"
  },
  "pay_status_valuetotext": {
    "0": "未支付",
    "1": "已支付",
    "2": "部分退款",
    "3": "全额退款"
  },
  "evaluate_status_valuetotext": {
    "0": "未评价",
    "1": "已评价"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
